package net.edu_soft.ec.common.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.edu_soft.ec.common.exception.OperationException;
import net.edu_soft.ec.common.exception.ParameterException;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.util.Assert;

/**
 * @author puing
 * @Description: 工具类
 * @date 2019年11月27日
 */
public class PuingUtil {

    public static String[] chars = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
            "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
            "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    /**
     * @return 随机码
     * @Title: generateShortUuid
     * @Description: 生成8位UUID大写随机码
     */
    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        //得到随机值
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        //转换大写
        uuid = shortBuffer.toString().toUpperCase();
        return uuid;
    }

    /**
     * @return 16位随机码
     * @Title: getRandomIdByUtil
     * @Description: 生成16位UUID随机码
     */
    public static String getRandomIdByUtil() {
        String randomId = UUID.randomUUID().toString().replace("-", "");
        return randomId;
    }

    /**
     * @param number
     * @param message
     * @Description: 数字不为空且大于0
     */
    public static void notNullByZero(Integer number, String message) {
        if (number == null || number <= 0) {
            throw new ParameterException(message);
        }
    }


    /**
     * @param message
     */
    public static void notNull(String message) {
        throw new ParameterException(message);
    }


    /**
     * @param numbers 数值
     * @param message 消息
     * @Description: 数字不为空且大于0
     */
    public static void notNullByInt(String message, Integer... numbers) {

        for (Integer number : numbers) {
            if (number == null || number <= 0) {
                throw new ParameterException(message);
            }

        }
    }

    /**
     * @param message
     * @param numbers
     * @Description: 数字不为空且大于0
     */
    public static void notNullByZeroLong(String message, Long... numbers) {
        //遍历
        for (Long number : numbers) {
            //判断
            if (number == null || number <= 0) {
                throw new ParameterException(message);
            }
        }
    }

    /**
     * @param numbers
     * @Description: 数字不为空
     */
    public static void notNullLong(Long... numbers) {
        //遍历
        for (Long number : numbers) {
            //判断
            if (number == null) {
                throw new ParameterException("参数不能为空");
            }
        }
    }

    public static void notNullString(String... strs) {
        for (String str : strs) {
            //判断
            Assert.hasLength(str.trim(), "参数不能为空");
        }
    }


    /**
     * @param str 带数字的字符串
     * @return 提取后的整数集合
     * @Description: 截取字符串并提取数字
     */
    public static List<Integer> getNumber(String str) {
        Assert.hasLength(str.trim(), "字符数据不能为空");
        List<Integer> list = new ArrayList<Integer>();
        String regEx = "[^0-9]";
        Pattern p = Pattern.compile(regEx);
        String[] strs = str.split(",");
        for (int i = 0, len = strs.length; i < len; i++) {
            Matcher m = p.matcher(strs[i].toString());
            list.add(Integer.parseInt(m.replaceAll("").trim()));
        }
        return list;
    }


    /**
     * 提取字符串
     *
     * @param str 字符串
     * @return
     */
    public static List<String> getStr(String str) {
        if (str == null || StringUtils.isEmpty(str.trim())) {
            throw new ParameterException("要提取字符串为空！");
        }
        List<String> list = new ArrayList<>();
        String[] strs = str.split(",");
        for (String string : strs) {
            list.add(string.trim());
        }
        return list;
    }

    /**
     * 加密
     *
     * @return 加密过后的密码
     * @Title: getMD5Value
     */
    public static String getMD5Value(String password, String salt) {
        Object hash = new SimpleHash("MD5", password.trim(), salt.trim(), 49);
        return hash.toString();
    }

    /***
     * 判断list集合是否为空。为空抛出异常
     * @param T
     * @param message
     * @param <T>
     */
    public static <T> void notNullByList(List<T> T, String message) {
        if (T.size() == 0 || T == null) {
            throw new OperationException(message);
        }
    }

    /***
     * 判断是否为空
     * @param message 错误消息
     * @param str 字符串
     */
    public static void isEmpty(String message, String str) {

        if (str == null || StringUtils.isEmpty(str.trim())) {
            if (message == null || StringUtils.isEmpty(message.trim())) {
                throw new ParameterException("输入的字符为空");
            } else {
                throw new ParameterException(message);
            }
        }
    }

    /***
     * 提取判断存在则追加
     * @param str1 原数据
     * @param str2 要添加的数据
     * @return 追加后的新集合
     */
    public static List<String> checkAndAppend(String str1,String str2){
        int flag = 0;
        List<String> str1_list = getStr(str1);//原集合
        List<String> str2_list = getStr(str2);//要比较的集合
        for (String str2_data : str2_list) {//遍历要比较的集合
            for (String str1_data : str1_list) {//遍历原集合
                if(str2_data.equals(str1_data)) {//用要比较的字符串与原集合的元素比较，正确则为true
                    flag=1;
                }
            }
            if(flag==0) {//不同为0则往原集合追加
                str1_list.add(str2_data);
            }
            flag = 0;//重置鉴别条件
        }

        return str1_list;//返回

    }

    /***
     * 根据字符串集合转化为字符串，以英文“ , ”分隔
     * @param list 要转化的集合
     * @return
     */
    public static String convertStrings(List<String> list) {
        StringBuffer sb = new StringBuffer();
        for (String id : list) {
            sb.append(id + ",");
        }
        return sb.toString();
    }


    /***
     * 得到当前年月的字符串格式
     * @return
     */
    public static String getYearMonth() {
        Calendar c1 = Calendar.getInstance();// 使用默认时区和区域设置获取日历。
        String year = String.valueOf(c1.get(Calendar.YEAR));// 得到年
        String month = null;
        int m = c1.get(Calendar.MONTH) + 1;// 得到月
        if (m == 10 || m == 11 || m == 12) {//格式化
            month = m + "";
        }else {
            month="0"+m;
        }
        return year+month;
    }


//	public static void main(String[] args) {
//		String randomId = getRandomIdByUtil();
//		System.out.println(randomId);
//	}


}
